﻿@using Grand.Business.Core.Interfaces.Common.Directory
@using Grand.Business.Core.Interfaces.Common.Security
@using Grand.Domain.Permissions
@inject IGroupService groupService
@inject IWorkContextAccessor workContextAccessor
@inject IPageHeadBuilder pagebuilder
@inject IPermissionService permissionService
@{
    var registerUrl = Url.RouteUrl("Register", new { returnUrl = Context.Request.Query["returnUrl"] });
    var isAuthenticated = await groupService.IsRegistered(workContextAccessor.WorkContext.CurrentCustomer);
    var isCustomerImpersonated = workContextAccessor.WorkContext.OriginalCustomerIfImpersonated != null;
    var isAdminAccess = await permissionService.Authorize(StandardPermission.ManageAccessAdminPanel);
    var isVendor = workContextAccessor.WorkContext.CurrentVendor != null;
}

@await Component.InvokeAsync("Widget", new { widgetZone = "header_links_before" })
<div>
    @if (isCustomerImpersonated)
    {
        <a class="btn btn-outline-secondary w-100 mb-2" href="@Url.RouteUrl("Logout")" title="@Loc["Account.ImpersonatedAs.Finish.Hint"]">
            <b-icon icon="door-closed"></b-icon>
            <span class="text text-uppercase ml-3">@string.Format(Loc["Account.ImpersonatedAs"], workContextAccessor.WorkContext.CurrentCustomer.Email ?? "GUEST")</span>
        </a>
    }
    @if (isAuthenticated)
    {
        @if (isAdminAccess && !isVendor)
        {
            <a class="btn btn-outline-secondary w-100 mb-2" href="@Url.Content("~/Admin")">
                <b-icon icon="gear"></b-icon>
                <span class="text text-uppercase ml-3">@Loc["admin"]</span>
            </a>
        }

        @if (isVendor)
        {
            <a class="btn btn-outline-secondary w-100 mb-2" href="@Url.Content("~/Vendor")">
                <b-icon icon="gear"></b-icon>
                <span class="text text-uppercase ml-3">@Loc["vendor.portal"]</span>
            </a>
        }

        @if (!string.IsNullOrEmpty(pagebuilder.EditPageUrl))
        {
            <a target="_blank" class="btn btn-outline-secondary w-100 mb-2" href="@pagebuilder.EditPageUrl">
                <b-icon icon="wrench"></b-icon>
                <span class="text text-uppercase ml-3">@Loc["edit"]</span>
            </a>
        }

        <a class="btn btn-outline-secondary w-100 mb-2" href="@Url.RouteUrl("CustomerInfo")">
            <b-icon icon="person"></b-icon>
            <span class="text text-uppercase ml-3">@Loc["Account.MyAccount"]</span>
        </a>
        <a class="btn btn-outline-secondary w-100 mb-2" href="@Url.RouteUrl("CustomerOrders")">
            <b-icon icon="handbag"></b-icon>
            <span class="text text-uppercase ml-3">@Loc["Account.CustomerOrders"]</span>
        </a>
        <a class="btn btn-outline-secondary w-100 mb-2" href="@Url.RouteUrl("Logout")">
            <b-icon icon="lock"></b-icon>
            <span class="text text-uppercase ml-3">@Loc["Account.Logout"]</span>
        </a>
    }
    else
    {
        <span class="d-flex h5 justify-content-center mb-3 w-100">@Loc["Account.Login.ReturningCustomer"] / @Loc["SideBar.NewCustomer"]</span>
        <div class="d-inline-flex flex-sm-nowrap flex-wrap w-100">
            <a class="btn btn-secondary w-100 mx-1 mb-2" href="@Url.RouteUrl("Login")">
                <b-icon icon="shield-lock"></b-icon>
                <span class="text text-uppercase ml-sm-3 ml-2">@Loc["Account.Login"]</span>
            </a>
            <a class="btn btn-secondary w-100 mx-1 mb-2" href="@registerUrl">
                <b-icon icon="person"></b-icon>
                <span class="text text-uppercase ml-sm-3 ml-2">@Loc["Account.Register"]</span>
            </a>
        </div>
        <div class="text-center generalMarginSupporter">
            @Loc["Account.Login.NewCustomerText"]
        </div>
    }
</div>
@await Component.InvokeAsync("Widget", new { widgetZone = "header_links_after" })